package cn.gov.anhai.service;

import cn.gov.anhai.mappers.PublicMapper;
import cn.gov.anhai.utils.MyUtil;
import cn.gov.anhai.vo.OssVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PublicService {
    @Resource
    PublicMapper publicMapper;

    static Map<Long, String> mapOSS = new HashMap<>();
    static Map<Long, OssVO> mapOssInfo = new HashMap<>();

    /**
     * 根据编号获得OSS地址
     */
    public String getOSSUrl(Long id) {
        if (id == null) return null;
        if (!MyUtil.hasMapKey(mapOSS, id)) {
            System.out.println("查图片=" + id);
            String img = publicMapper.getOSSUrl(id);
            mapOSS.put(id, StringUtils.isEmpty(img) ? "" : img);
        }
        return mapOSS.get(id);
    }

    public OssVO getOssInfo(Long id) {
        if (id == null) return null;
        if (!MyUtil.hasMapKey(mapOssInfo, id)){
            System.out.println("查oss=" + id);
            OssVO ossVO = publicMapper.getOssInfo(id);
            mapOssInfo.put(id, ossVO);
            return ossVO;
        }
        return mapOssInfo.get(id);
    }

    /**
     * 根据编号集合获得OSS地址
     */
    public List<String> getOSSUrl(String id) {
        if (StringUtils.isEmpty(id)) return new ArrayList<>();
        String[] ids = id.split(",");
        List<String> list = new ArrayList<>();
        for (String item : ids) {
            list.add(getOSSUrl(Long.parseLong(item)));
        }
        return list;
    }

    /**
     * 获得打开数量
     */
    public Integer getDynamicCount(Long id) {
        Integer count = publicMapper.getDynamicCount(id);
        return count == null ? 0 : count;
    }

    /**
     * 判断是否点赞或收藏
     *
     * @param uid        用户编号
     * @param type       操作类型（1点赞 2收藏 3景点收藏）
     * @param dynamic_id 关联编号
     */
    public boolean isUserDynamic(Long uid, Integer type, Long dynamic_id) {
        Integer count = publicMapper.getUserDynamicCount(uid, type, dynamic_id);
        return count != null && count > 0;
    }

    /**
     * 操作点赞或收藏
     *
     * @param uid        用户编号
     * @param type       操作类型（1点赞 2收藏 3景点收藏）
     * @param dynamic_id 关联编号
     */
    public Map<String, Object> doUserDynamic(Long uid, Integer type, Long dynamic_id, String stateName) {
        boolean state = isUserDynamic(uid, type, dynamic_id);
        if (state) {
            Integer result = publicMapper.deleteUserDynamic(uid, type, dynamic_id);
            if (result != 1) return null;
            return getUserDynamic(uid, type, dynamic_id, stateName);
            //data.put("count", 0);
            //data.put("isCollect", false);
        } else {
            Integer result = publicMapper.addUserDynamic(uid, type, dynamic_id);
            if (result != 1) return null;
            return getUserDynamic(uid, type, dynamic_id, stateName);
            //data.put("count", 1);
            //data.put("isCollect", true);
        }
    }

    /**
     * 获得是否点赞/收藏和总条数
     *
     * @param uid        用户编号
     * @param type       操作类型（1点赞 2收藏 3景点收藏）
     * @param dynamic_id 关联编号
     * @param stateName  状态名称
     * @return
     */
    public Map<String, Object> getUserDynamic(Long uid, Integer type, Long dynamic_id, String stateName) {
        boolean state = uid == null ? false : isUserDynamic(uid, type, dynamic_id);
        Integer count = publicMapper.getUserDynamicCount(null, type, dynamic_id);
        return new HashMap<String, Object>() {
            {
                put("count", count);
                put(stateName, state);
            }
        };
    }

    /**
     * 获得默认头像
     */
    public Long getUserDefaultAvatar() {
        String data = publicMapper.getConfigByKey("default_avatar_oss_id");
        if (data == null) return null;
        try {
            return Long.parseLong(data);
        } catch (Exception ex) {
            return null;
        }
    }
}
